/*
 * @Author: Jimmy
 * @LastModifiedBy: Jimmy
 * @Date: 2024-02-23 19:27:27
 * @LastEditTime: 2024-02-24 19:16:42
 * @FilePath: /react-vite-admin/src/styles/theme/index.ts
 */
import { defaultTheme, defaultThemeToken } from "./default";
import { lightTheme, lightThemeToken } from "./light";
import { blueTheme, blueThemeToken } from "./blue";
import cssVars from "css-vars-ponyfill";

export const initTheme = (themeType: string) => {
    document.documentElement.setAttribute('data-theme', themeType);
    let { options, theme } = switchTheme(themeType);
    cssVars({
        watch: true,// 当添加，删除或修改其<link>或<style>元素的禁用或href属性时，ponyfill将自行调用
        variables: options,
        onlyLegacy: true,// false  默认将css变量编译为浏览器识别的css样式  true 当浏览器不支持css变量的时候将css变量编译为识别的css
    });
    return theme;
}
//处理主题
const switchTheme = (type: string) => {
    let options, theme;
    switch (type) {
        case 'default':
            options = defaultTheme;
            theme = defaultThemeToken;
            break;
        case 'light':
            options = lightTheme;
            theme = lightThemeToken;
            break;
        case 'blue':
            options = blueTheme;
            theme = blueThemeToken;
            break;
        default:
            options = defaultTheme;
            theme = defaultThemeToken;
            break;
    }
    return {
        options,
        theme
    };
}